/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.ui.client.actions;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import org.opu.pc.services.PublisherService;
import org.opu.pc.services.beans.AbstractBean;
import org.opu.pc.services.beans.PublicationBean;
import org.opu.pc.services.beans.PublisherBean;
import org.opu.pc.ui.client.MainView;
import org.opu.pc.ui.util.Options;

/**
 *
 * @author yura
 */
public class PublicationPublishers extends AbstractAction 
		implements SerialAction, PropertyChangeListener {

	private static final long serialVersionUID = 9081999070507728063L;
	
	private PublisherService publisherService;
    private PublicationBean lastPublication;
    private boolean canBeAnable = false;

    public void setPublisherService(PublisherService publisherService) {
        this.publisherService = publisherService;
    }

    @Override
    public String getName() {
        return "PublicationPublishers"; // the publishers of current publication
    }

    @Override
    public void runAction(ActionEvent e) throws Exception {
        List<? extends AbstractBean> selected = mainView.getSelectedBeans();
        if (selected == null || selected.size() != 1){
            Options.error(getSafety(getName() + ".err1", "You can perform this"
                    + " action only if you select ONE publication"));
            return;
        }

        AbstractBean get = selected.get(0);
        if (!(get instanceof PublicationBean)){
            Options.error(getSafety(getName() + ".err2", "You have to select"
                    + " publication (not publishers)"));
            return;
        }

        lastPublication = (PublicationBean) get;
        publishersForPublication(lastPublication);
    }

    @Override
    public void runAgaion() {
        publishersForPublication(lastPublication);
    }

    public void publishersForPublication(PublicationBean p){
        Class<PublisherBean> c = PublisherBean.class;
		if (p != null) {
            mainView.setContent(publisherService.publishersForPublication(p), c);
        } else {
            logger.warning("No publication - set empty data");
            mainView.setContent(new ArrayList<PublisherBean>(), c);
        }
    }

	@Override
	public void setMainView(MainView mainView) {
		super.setMainView(mainView);
		mainView.addChangeListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (MainView.SELECTED_BEANS.equals(evt.getPropertyName())){
			List list = (List) evt.getNewValue();
			setEnabled((list != null && list.size() == 1) && canBeAnable);
		} else if (MainView.CONTENT_TYPE.equals(evt.getPropertyName())){
			canBeAnable = (PublicationBean.class.equals(evt.getNewValue()));
		}
	}
}
